package g16.keephealthy;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {

	//variaveis da actividade necessarias para calculo do imc e outras
	Button Button01,Button02,Button03,Button04,Button05, del;
	private boolean doubleBackToExitPressedOnce;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
        String filename = "data.txt";
        
        File file = getBaseContext().getFileStreamPath(filename);
        // se ficheiro data nao existir vai para start activity 
        if(!file.exists())
        {
        	Intent TrocaEcran = new 
    		Intent(MainActivity.this,StartActivity.class);
    		MainActivity.this.startActivity(TrocaEcran);
    		MainActivity.this.finish();
        }
        
        //atribui button aos buttons no menu layout
        del = (Button) findViewById(R.id.del);
        Button01 = (Button) findViewById(R.id.Button01);// calcula imc
        Button02 = (Button) findViewById(R.id.Button02);// exercicios
        Button03 = (Button) findViewById(R.id.Button03);// historico
        Button04 = (Button) findViewById(R.id.Button04);// altera dados
        Button05 = (Button) findViewById(R.id.Button05);//info
        
        // on click no botao vai para a actividade respectiva
        Button01.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent TrocaEcran = new 
				Intent(MainActivity.this,CalculaActivity.class);
				MainActivity.this.startActivity(TrocaEcran);
				MainActivity.this.finish();

			}
		});
        
        
        Button02.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent TrocaEcran = new 
				Intent(MainActivity.this,ExerciciosActivity.class);
				MainActivity.this.startActivity(TrocaEcran);
				MainActivity.this.finish();
			}
		});
        
        
        Button03.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent TrocaEcran = new 
				Intent(MainActivity.this,EstatisticasActivity.class);
				MainActivity.this.startActivity(TrocaEcran);
				MainActivity.this.finish();

			}
		});
        
        
        Button04.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent TrocaEcran = new 
				Intent(MainActivity.this,Dados2Activity.class);
				MainActivity.this.startActivity(TrocaEcran);
				MainActivity.this.finish();

			}
		});
        
        Button05.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent TrocaEcran = new 
				Intent(MainActivity.this,InfoActivity.class);
				MainActivity.this.startActivity(TrocaEcran);
				MainActivity.this.finish();

			}
		}); 
        
        
        //on click apaga os ficheiros todos e sai da app
        del.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 String filedata = "data.txt";  
			     File filedataF = getBaseContext().getFileStreamPath(filedata);   
			     String fileexer = "exercicios.txt";   
			     File fileexerF = getBaseContext().getFileStreamPath(fileexer);
			     String filehist = "historico.txt";   
			     File filehistF = getBaseContext().getFileStreamPath(filehist);
			     
			     if(filedataF.exists())
			    	 filedataF.delete();
			     if(fileexerF.exists())
			    	 fileexerF.delete();			    
			     if(filehistF.exists())
			    	 filehistF.delete();

			     System.exit(0);
			     
			     Intent intent = new Intent(Intent.ACTION_MAIN);
			     intent.addCategory(Intent.CATEGORY_HOME);
			     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			     startActivity(intent);
			}
		});
        
        
        
    }

    //on back pressed pede segundo back para sair da app
    @Override
    public void onBackPressed() {
           
		if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Click BACK outra vez para sair", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce=false;                       
            }
        }, 2000);
    } 
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
